<?php
function outputBrowser ()
{
    global $cc;
    global $g_conf;
    $str = ob_get_contents();
    ob_end_clean();
    echo iconv("UTF-8","gbk//IGNORE",$str);
    ob_start();
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
include_once( "class/ccharset.php" );
$cc = new CCharset();
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$betname_3d = judge_type($gametype, true);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
if (isset($_SESSION['subaccount']))
{
    $ui->showErrorPage("你是子帐号，不能执行这个功能！");
    exit();
}
global $curruser;
$ui = new UI_3D();
$tpl = new Template2();
require( "class_3d/Bet_3d.php" );
require( "class_3d/RateConfig_3d.php" );
$Bet_3d = new Bet_3d($gametype);
$RateConfig_3d = new RateConfig_3d($gametype);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用", true);
    exit();
}
$userlist = $Bet_3d->getMyMemberIdByAcl($curruser['id'], $curruser['acl']);
if ($userlist == "")
{
    $ui->showErrorPage("还没有建立会员！", true);
    exit();
}
switch ($show3D3S)
{
    case 0 :
        $paperOpen = date("Y-m-d");
        break;
    case 1 :
        $paperOpen = $RateConfig_3d->getPaperOpen();
        break;
}
$betcount = 0;
$rs = getdb("select id,round from lotto_{$tbl} where open='{$paperOpen}' order by open");
$lottoIdvsRound = array();
if (!$rs->eof)
{
    switch ($show3D3S)
    {
        case 0 :
            $lotto_id = $rs->f[0];
            $round = $rs->f[1];
            $rs = getdb("select count(id) from bet_{$tbl} where lotto_id={$lotto_id}");
            $betcount = $rs->f[0];
            $whereStr = "lotto_id={$lotto_id}";
            $lottoIdvsRound[$lotto_id] = $round;
            break;
        case 1 :
            $lottoArr = array();
            while (!$rs->eof)
            {
                array_push($lottoArr, $rs->f[0]);
                $lottoIdvsRound[$rs->f[0]] = $rs->f[1];
                $rs->next();
            }
            $whereStr = " lotto_id IN (" . implode(",", $lottoArr) . ")";
            $rs = getdb("select count(id) from bet_{$tbl} where {$whereStr}");
            $betcount = $rs->f[0];
            break;
    }
}
if ($betcount == 0)
{
    $ui->showErrorPage("抱歉，本期没有任何{$tblName}注单资料！", true);
    exit();
}
$rs->close();
$dataname = G_DBDATABASE;
while (@ob_end_clean())
{
    
}
ob_start();
header("Pragma: no-cache");
header("Expires: 0");
header("Content-disposition: attachment; filename=" . substr($dataname, 0, 1) . "_" . substr($dataname, 0 - 1) . "-" . date("Ymd") . "_{$tbl}.csv");
header("Content-type: plain/text");
$Year = date("Y");
$Month = date("m");
$Day = date("d");
$acl_name = array("", "会员", "代理", "总代", "股东", "大股");
$search = array("\x00", "\n", "\r", "\x1A");
$replace = array("\\0", "\\n", "\\r", "\\Z");
$DataCreation = strftime("%d %B %Y", mktime(0, 0, 0, $Month, $Day, $Year));
$Time = date("H:i:s");
$crlf = "\r\n";
$i = 0;
$schema_insert = "";
$table = "";
$schema_create = "";
$betArr = array("bet_{$tbl}", "bet2_{$tbl}");
foreach ($betArr as $key => $table)
{
    echo "" . $crlf . "";
    if ($table == "bet_{$tbl}")
    {
        echo "{$tblName}会员注单明细表" . $crlf . "";
        echo "单号,会员帐号,期数,时间,下注内容,金额,赔率,盘类,IP" . $crlf . "";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.bettype,B.subid,B.line_nums,B.amount,B.rate,B.disk,B.ip";
    }
    else
    {
        echo "{$tblName}管理走飞明细表" . $crlf . "";
        echo "单号,管理帐号,期数,时间,阶层,走飞内容,金额,赔率,IP,模式" . $crlf . "";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.acl,B.bettype,B.subid,B.line_nums,B.amount,B.rate,B.ip,B.rame";
    }
    outputBrowser();
    if ($curruser['acl'] == 2)
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM {$table} B LEFT JOIN k_user K ON K.id = B.user_id WHERE {$whereStr} and B.user_id in ({$userlist}) order by B.id desc") ))
        {
            exit("Can't select data from {$table} 1");
        }
    }
    else if ($table == "bet_{$tbl}")
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM {$table} B LEFT JOIN k_user K ON K.id = B.user_id WHERE {$whereStr} and B.user_id in ({$userlist}) order by B.id desc") ))
        {
            exit("Can't select data from {$table} 2");
        }
    }
    else
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM {$table} B LEFT JOIN k_user K ON K.id = B.user_id WHERE {$whereStr} and B.uperid" . $curruser['acl'] . "=" . $curruser['id'] . " and B.user_id!=" . $curruser['id'] . " order by B.id desc") ))
        {
            exit("Can't select data from {$table} 3");
        }
    }
    $a = 0;
    $schema_insert = "";
    while ($row = mysql_fetch_row($result))
    {
        $schema_insert = "";
        $fqty = mysql_num_fields($result);
        $j = 0;
        while ($j < $fqty)
        {
            if (mysql_field_name($result, $j) == "lotto_id")
            {
                $schema_insert .= " " . $lottoIdvsRound[$row[$j]] . ",";
            }
            else if (mysql_field_name($result, $j) == "createtime")
            {
                $schema_insert .= " " . date("m-d H:i:s", $row[$j]) . ",";
            }
            else if (mysql_field_name($result, $j) == "acl")
            {
                $schema_insert .= " " . $acl_name[$row[$j]] . ",";
            }
            else if (mysql_field_name($result, $j) == "bettype")
            {
                $tempIdx = $row[$j] . "_" . $row[$j + 1];
                $schema_insert .= isset($betname_3d[$tempIdx]) ? " " . $betname_3d[$tempIdx] . " " . $row[$j + 2] . "," : "-- ,";
            }
            else if (mysql_field_name($result, $j) == "subid" || mysql_field_name($result, $j) == "line_nums")
            {
                $schema_insert .= "";
            }
            else if (mysql_field_name($result, $j) == "disk")
            {
                $schema_insert .= " " . $g_disk_code[$row[$j]] . ",";
            }
            else if (!isset($row[$j]))
            {
                $schema_insert .= " NULL,";
            }
            else if ($row[$j] != "")
            {
                $schema_insert .= " " . str_replace($search, $replace, addslashes($row[$j])) . ",";
            }
            else
            {
                $schema_insert .= " ,";
            }
            ++$j;
        }
        $schema_insert = ereg_replace(",\$", "", $schema_insert);
        $schema_insert .= "" . $crlf;
        echo $schema_insert;
        ++$a;
        outputBrowser();
    }
    ++$i;
}
echo "--------------------------------------------END--------------------------------------------------------" . $crlf . "";
?>